
import { Component, OnInit, Injector, Input, ViewChild, AfterViewInit } from '@angular/core';
import { ModalComponentBase } from '@shared/component-base/modal-component-base';
import { FormBuilder, FormGroup } from '@angular/forms';
import {
  CreateOrUpdateStockInput,
  SelectListItem,
  StockEditDto,
  StockItem,
  StockServiceProxy
} from '@shared/service-proxies/service-proxies';
import { Validators, AbstractControl, FormControl } from '@angular/forms';
import { finalize } from 'rxjs/operators';
import { Console } from 'console';

@Component({
  selector: 'create-or-edit-stock',
  templateUrl: './create-or-edit-stock.component.html',
  styleUrls: [
    'create-or-edit-stock.component.less'
  ],
})

export class CreateOrEditStockComponent
  extends ModalComponentBase
  implements OnInit {
  
  listOfControl: Array<StockItem>=[];
  validateForm!: FormGroup;
  tModel: number;
  /**
  * 编辑时DTO的id
  */
  id: any;

  entity: StockEditDto = new StockEditDto();

  categoryList: Array<SelectListItem>;

  typeList: Array<SelectListItem>;

  statusList: Array<SelectListItem>;
  /**
  * 构造函数，在此处配置依赖注入
  */
  constructor(
    injector: Injector,
    private _stockService: StockServiceProxy,
    private fb: FormBuilder
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.init();
    this.validateForm = this.fb.group({});
  }


  /**
  * 初始化方法
  */
  init(): void {
    this._stockService.getForEdit(this.id).subscribe(result => {
      this.entity = result.stock;
      this.categoryList = result.categoryList;
      this.typeList = result.typeList;
      this.statusList = result.statusList;
      console.log(result.stockItems)
      if (!result.stockItems) {
        this.addField();
        this.addField();
        this.addField();
        this.addField();
      }
      else { 
        this.listOfControl = result.stockItems;
        for (let index = 0; index < result.stockItems.length; index++) {
          if (result.stockItems[index].isRight) {
            this.tModel = index ;
            break;
          }
        }
        console.log(this.tModel)
      }
    });
  }

  /**
  * 保存方法,提交form表单
  */
  submitForm(): void {
    const input = new CreateOrUpdateStockInput();
    input.stock = this.entity;

    this.saving = true;
    if (this.entity.type == '1') {
      for (let index = 0; index < this.listOfControl.length; index++) {
        if (index == this.tModel) {
          this.listOfControl[index].isRight = true;
        }
        else { 
          this.listOfControl[index].isRight = false;
        }
      }
    }
    input.stockItems = this.listOfControl;
    this._stockService.createOrUpdate(input)
      .pipe(finalize(() => (this.saving = false)))
      .subscribe(() => {
        this.notify.success(this.l('SavedSuccessfully'));
        this.success(true);
      });
  }

  set_type() {
    console.log(this.entity.type)
  }

  //删除一个选项
  removeField(i: StockItem, e: MouseEvent): void {
    e.preventDefault();
    if (this.listOfControl.length > 2) {
      const index = this.listOfControl.indexOf(i);
      this.listOfControl.splice(index, 1);
      console.log(this.listOfControl);
      this.validateForm.removeControl(i.title);
    }
  }

  //添加一个选项
  addField(e?: MouseEvent): void {
    if (e) {
      e.preventDefault();
    }
    const control = new StockItem();
    const index = this.listOfControl.push(control);
    this.validateForm.addControl(
      this.listOfControl[index - 1].title,
      new FormControl(null, Validators.required)
    );
  }


}
